#!/bin/bash
#简单功能的shell脚本
list_function=( "初始化" "部署Apache服务" "部署MySQL服务" "退出" )
menu() {
for ((i=0; i<${#list_function[@]}; i++ ));
do
    echo -e "\e[32m\t 选项$(($i+1))----${list_function[$i]}\e[0m"
done
}
useage() {
echo -e "\e[33m请输入选项1-${#list_function[@]}\e[0m"
}
date() {
old_time=$(( RANDOM % 10 ))
new_time=0.${old_time}
sleep $new_time
}
began() {
echo -e "\e[33m开始进行初始化\e[0m"
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null 
if [ $? -eq 0 ] ;then
   reposity_result=0
   echo -e "\e[32m更换阿里云源仓库成功\e[0m"
else 
   echo -e "\e[31m更换阿里云源仓库失败\e[0m"
fi 
yum -y install vim wget epel-release net-tools &> /dev/null
if [ $? -eq 0 ] ;then
   yum_result=0
   echo -e "\e[32m更换阿里云源仓库成功\e[0m"
   echo -e "\e[32m常用软件安装成功\e[0m"
else 
   echo -e "\e[31m常用软件安装失败\e[0m"
fi 
systemctl disable --now firewalld && sed -i 's/enforcing/disables/g' /etc/selinux/config
if [ $? -eq 0 ] ;then
   firewall_result=0
   echo -e "\e[32m更换阿里云源仓库成功\e[0m"
   echo -e "\e[32m防火墙关闭成功\e[0m"
else 
   echo -e "\e[31m防火墙关闭失败\e[0m"
fi 
if [[ $reposity_result -eq 0  &&  $yum_result -eq 0  &&  $firewalld_result -eq 0 ]] ;then
   echo -e "\e[32m初始化成功\e[0m"
else 
   echo -e "\e[31m初始化失败\e[0m"
fi
echo -e "\e[33m初始化结束\e[0m"
}
set_apache() {
echo -e  "\e[33m开始部署Apache服务\e[0m"
systemctl status httpd &> /dev/null
if [ $? -eq 0 ];then
   echo -e "\e[32mApache服务正常运行中\e[0m"
else 
   yum -y install httpd &> /dev/null 
   if [ $? -eq 0 ];then 
      systemctl enable --now httpd &> /dev/null
      if [ $? -eq 0 ];then
         echo -e "\e[32mApache服务启动成功\e[0m"
      else
         echo -e "\e[33mApache服务未成功启动\e[0m" 
      fi
   else
      echo -e "\e[33mApache服务未成功安装\e[0m"
   fi
fi
wget www.jd.com -O /var/www/html/index.html &> /dev/null
if [ $? -eq 0 ];then
   echo -e "\e[32m京东页面已经成功部署到Apache默认发布目录\e[0m"
   echo -e "\e[33mApache服务部署完毕\e[0m"
else 
   echo -e "\e[33m京东页面部署失败\e[0m"
fi
}
set_mysql() {
echo -e "\e[32m开始安装MySQL\e[0m"
yum -y install yum-utils &> /dev/null
yum -y  remove `rpm -qa | grep "mariadb"` &> /dev/null
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm &> /dev/null
rpm -ivh mysql57-community-release-el7-11.noarch.rpm &> /dev/null
yum-config-manager --disable mysql80-community &> /dev/null
sed -i 's/gpgcheck=1/gpgcheck=0/' /etc/yum.repos.d/mysql-community.repo 
yum -y install mysql mysql-server &> /dev/null
if [ $? -eq 0 ];then
   echo -e "\e[32mMySQL服务安装完毕！\e[0m"
else 
   echo -e "\e[33mMySQL服务未能成功安装\e[0m"
   exit
fi
default_password=`grep password /var/log/mysqld.log | awk -F":" '{print $NF}' | tr -d " \n"`
read -sp "输入你要进行使用的新密码: " password
mysqladmin -uroot -p"$default_password" password $password 2> /dev/null
if [ $? -eq 0 ];then
   echo -e  "\e[33m密码修改完成，新密码为: ${password}\e[0m"
   mysql -uroot -p$password -e "exit" 2> /dev/null
   if [ $? -eq 0 ];then
      echo -e "\e[32mMySQL服务初始化完成，新的root密码为: $password\e[0m"
   else 
      echo -e "\e[32mMySQL密码错误\e[0m"
   fi
else
   echo -e "\e[31mMySQL密码未能成功更改\e[0m"
fi
}
main() {
while true; 
do
   menu
   read -p "请输入你要使用的功能" choice
   case $choice in 
     1)
        began
        date;;
     2) 
        set_apache
        date;;
     3) 
        set_mysql
        date;;
     4) 
        exit ;;
     *) 
        useage ;;
   esac
done
}
main
